package com.test.test_002;

import java.util.TreeSet;

public class Person implements Comparable<Person> {
	private int age;
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}

	public String toString() {
		return "name:" + name + "," + "age:" + age;
	}

	@Override
	public int compareTo(Person o) {
		int oAge = o.getAge();
		if (age > oAge) {
			return 1;
		} else if (age < oAge) {
			return -1;
		} else {
			return name.compareTo(o.getName());
			// return o.getName().compareTo(name);
		}
	}

	public int hashCode() {
		return name.hashCode();
	}

	/**
	 * TODO compareTo 升序：age > o.age 返回1 为升序 age < o.age 返回-1为降序
	 */
	public static void main(String[] args) {
		// Person person = new Person(12, "aa");
		// Person[] persons = { new Person(12, "aa"), new Person(13, "bb"),
		// new Person(13, "aa"), new Person(12, "cc") };
		// System.out.println(Arrays.toString(persons));
		// // Arrays.sort(persons);
		// Collections.sort(Arrays.asList(persons));
		// System.out.println(Arrays.toString(persons));

//		TreeMap<Person, String> persons = new TreeMap<Person, String>();
//		persons.put(new Person(12,"aa"), "001");
//		persons.put(new Person(12,"bb"), "002");
//		persons.put(new Person(13,"bb"), "003");
//		persons.put(new Person(13,"aa"), "004");
//		System.out.println(persons);
		
		
		TreeSet<Person> persons = new TreeSet<Person>();
		persons.add(new Person(12,"aa"));
		persons.add(new Person(12,"bb"));
		persons.add(new Person(13,"bb"));
		persons.add(new Person(13,"aa"));
		System.out.println(persons);
	}

}
